package com.example.jack.community.view.me;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.jack.community.R;
import com.example.jack.community.data.me.Images;
import com.example.jack.community.view.me.adapter.IMageAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 我的社团页面
 * 我参加的社团fragment片段
 * * A simple {@link Fragment} subclass.
 */
public class MyComFragment extends Fragment {

    //我的社团recyclerview
    @BindView(R.id.my_com_recycler)
    RecyclerView myComRecycler;

    Unbinder unbinder;

    //下拉刷新控件
    @BindView(R.id.swip)
    SwipeRefreshLayout swip;

    //
    private List<Images> list = new ArrayList<>();

    public MyComFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        initFruits();
        View view = inflater.inflate(R.layout.fragment_my_com2, container, false);
        unbinder = ButterKnife.bind(this, view);
        LinearLayoutManager layout = new LinearLayoutManager(getActivity());
        myComRecycler.setLayoutManager(layout);
        final IMageAdapter adapter = new IMageAdapter(list);
        myComRecycler.setAdapter(adapter);

        swip.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        adapter.setOnItemClickLitener(new IMageAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getActivity(), "这是条目" + position, Toast.LENGTH_LONG).show();
            }
        });

        //添加下拉刷新监听
        swip.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //实际开发中，我们在这里发送网络请求获取数据
                list.add(new Images(R.drawable.setting_a2));
                adapter.notifyDataSetChanged();
//                //隐藏刷新动画
                swip.setRefreshing(false);
            }
        });
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initFruits() {
        for (int i = 0; i < 3; i++) {
            Images fruit = new Images(R.drawable.setting_a4);
            list.add(fruit);
        }
    }
}
